//视频上传
var vueApp = new Vue({
  el: '#app',
  data: function () {
	return {  
    	id:'',
    	fileNo:'',
		title: '',
		rmk: '',  
		uploadUrl:'../../pub/attach/uploadsingle',
    	data:{},  
		typeLimit:['image/jpg','image/jpeg','image/png','application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    	           'application/msword','text/plain','video/mp4','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],        
        fileData:[],    
        showDialog: false,
        isPic: false,
        media_src:'',
        media_type:'',
        oper:''
    };
  },
  mounted: function () {
    this.initData();
  },
  methods: {
	  initData:function(){
		  var that = this;
		  this.id = tools.getQueryString("id");
		  if(this.id!=""){
			  this.oper = "edit";	
			  var jsonStr = {
				"id": this.id	  
			  };
			  var reqData ={
			    "jsonStr": JSON.stringify(jsonStr)
			  };
			  tools.ajaxRequestAsyncEx("pub/file/upfileInfo", reqData, function(data){
				  console.log(data);
				  if (data.respCode == "0") {
					  var info =data.list[0];
					  that.rmk = info.rmk;
					  that.title= info.title;
					  if(info.file_no!=''){
						  that.fileNo = info.file_no;
						  that.onSearch();
					  }
			      }else{
			    	  that.showError(data.respDesc);      
			      }
			  });
			  
			             
		   }else{
			   this.oper = "add";
			   this.getMaxId();
		   }	 
	  },  
	  
	  check:function(){
		  	var that = this;
			if(this.title==""){
				this.showError("标题不能为空"); 
				return false;
			}
			if(this.fileNo==''){
				this.showError("请上传附件");
				return false;
			}
			var pic = false;
			var video = false;
			this.fileData.forEach(function(item,idx){
				if(that.getType(item)==0){
					pic = true;
				}else if(that.getType(item)==1){
					video = true;
				}
			});
			if(!pic){
				this.showError("请上传至少一张图片");
				return false;
			}
			if(!video){
				this.showError("请上传至少一个视频");
				return false;
			}
			
			return true;
	  },
	  save:function(){	
		  var that = this;
		  if(!this.check()){  
			  return false;
		  }		  
		  var jsonStr = {
			"id": this.id,
			"operType": this.oper,
			"title": this.title,
			"rmk":this.rmk,
			"fileNo": this.fileNo
		  };
		  var reqData ={
			"jsonStr": JSON.stringify(jsonStr)
		  };
		  tools.ajaxRequestAsyncEx("pub/file/upfileSave", reqData, function(data){
			if (data.respCode == "0") {
					that.$message({
		  	 	        showClose: true,
		  	 	        type: 'success',
		  	 	        message: "保存成功"
		  	 	    });	
				    setTimeout(() => {
				    	window.close();
			           }, 1000)
					
			}else{
				 that.showError(data.respDesc);      
			}
		  });	  
		  
		  
	  },
	  back:function(){
		  window.close();
	  },
	  onSearch:function(){	    
	    	var that =this;
	    	var jsonStr = {	
	    		"fileNo":this.fileNo
	    	};
	    	var reqData ={
	    	  "jsonStr": JSON.stringify(jsonStr),	    	
	    	  "pageSize": "20"
	    	};
	    	tools.ajaxRequestAsyncEx("pub/attach/uploadFileList", reqData, function(data){
	    	   if (data.respCode == "0") {
	    	       that.sortData(data.list);
	    	       that.fileData = data.list;
	    	   } else {
	    	       that.fileData = [];
	    	       that.totalRecords =0;
	    	       that.currentPageNbr=1;
	    	       that.showError(data.respDesc);      
	    	   }
	    	});
	    	
	  },
	  sortData:function(list){
		  var that = this;
		  list.sort(function sortType(o1,o2){
			  return that.getType(o1)-that.getType(o2);
		  });
	  },
	  getType:function(item){	// 0 图片	1 视频    2 文件
		  var type = item.file_type;
		  if(type.startsWith("image")){
			  return 0;
		  }else if(type.startsWith("video")){
			  return 1;
		  }else{
			  return 2;
		  }		  
	  },
	  view:function(item,type){
		  console.log(item);
		  if(type==0){
			  this.isPic = true;
		  }else{
			  this.isPic = false;
		  }		  
		  this.media_src = "../../"+item.file_path;
		  this.media_type = item.file_type;
		  this.showDialog = true;
	  },
	  download:function(item){
		  var url = "../../pub/attach/download?filepath="+item.file_path+"&filename="+item.file_name;
		  window.open(url);		  
	  },
	  del:function(item){
		  var that = this;
		  var reqData = {
			"fileId":item.file_id,
			"filePath":item.file_path
		  };
		  tools.ajaxRequestAsyncEx("pub/attach/fileDelete", reqData, function(data){
	    	   if (data.respCode == "0") {
	    		   that.$message({
	  	 	         showClose: true,
	  	 	         type: 'success',
	  	 	         message: "删除成功"
	  	 	      });
	    		  that.onSearch();
	    	   } else {
	    	       that.showError(data.respDesc);      
	    	   }
	      });	  
		  
	  },
	  getMaxId:function(){
		  var that=this;
		  var jsonStr={
				 
		  };
		  var reqData={
			"jsonStr":JSON.stringify(jsonStr)
		  };
		  tools.ajaxRequestAsyncEx("pub/file/getMaxId", reqData, function(data){
		   	  console.log(data);
			  if(data.respCode !=0){
		   			that.showError(data.respDesc);   
		   			return false;
			    }else{
			    	that.id = data.id;
  	            }			    
		  });		  
	  },  
	  beforeUploadFile: function(file) {	    	
		    this.data.fileName= file.name;
	    	this.data.fileType = file.type;
	    	this.data.fileSize = file.size;
	    	if(this.fileNo!=""){
	  		    this.data.fileNo = this.fileNo;
		    }		 
		    this.data.folder= "/file/upload";
	        this.data.modeName="文件上传";
	    	
	        const isLt10M = file.size / 1024 / 1024  < 500;
	        
	        if(file.type!="")
	        {
		        if (this.typeLimit.indexOf(file.type) == -1) {
		        	this.showError("上传文件格式不正确");
		            return false;
		        }
	        }
	        
	        if (!isLt10M) {
	        	this.showError("上传文件大小不能超过500MB!");
	            return false;
	        }
	  },
	  uploadFileProcess: function(event, file, fileList){
		  console.log(event);
		  var that = this;          
		  var complete = (event.loaded / event.total * 100 | 0);
          that.$showProgress(complete);
	  },
	  handleFileSuccess: function (res, file) {   
		  var that =this;
	      var fileNo = res.fileList[0].fileNo;
	      if(this.fileNo==""){
	    	  this.fileNo = fileNo;
	      }	  	      
//	      this.fileArgs.fileNo = this.fileNo;
//	      this.$refs.flist.refresh();	    
	      setTimeout(function(){
	    	  that.$showProgress(false);
	    	  that.$message({
	 	         showClose: true,
	 	         type: 'success',
	 	         message: "上传成功"
	 	      });
	    	  that.onSearch();
	      },2000);

	
	  },
	  handleFileError:function(err, file, fileList){
		  this.$showProgress(false);
	  },
   
      showError: function(info){
//        var i = (info==undefined?"加载数据错误":info);
//        this.$alert(i, '系统错误', {
//             confirmButtonText: '确定'
//        }); 
    	 _alert(info);
      },

   
  }
})